package com.gagakuai.bpm.api.testapi;

import com.gagakuai.bpm.dao.mapper.camunda.ActIdUserMapper;
import com.gagakuai.bpm.domian.entity.camuda.ActIdUser;
import com.gagakuai.web.encript.annonation.GagakuaiEncript;
import com.gagakuai.web.encript.domain.BaseResponseApapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/*
 * @program: settement
 * @description:
 * @author: houhong
 * @create: 2022-11-13 22:30
 */
@RestController
@RequestMapping("/hou")
public class MyTestContonroller {

    private List<DeferredResult<String>> deferredResultList = new ArrayList<>();

    @Autowired
    private ActIdUserMapper actIdUserMapper;

    @InitBinder
    public void initBinderDate(WebDataBinder binder) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CustomDateEditor dateEditor = new CustomDateEditor(df, true);
        binder.registerCustomEditor(Date.class, dateEditor);
    }

    @RequestMapping(value = "/param", method = RequestMethod.GET)
    public Map<String, Object> getFormatData(Date date) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "zhangsan");
        map.put("age", 22);
        map.put("date", date);
        return map;
    }

    @GetMapping("/add")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void testMethod() {

        ActIdUser actIdUser = new ActIdUser();
        actIdUser.setId("123434");
        actIdUser.setRev(1);
        actIdUser.setFirst("hou");
        actIdUser.setLast("hong");
        actIdUserMapper.insert(actIdUser);
        int i = 1 / 0;

    }

    @GetMapping("/hello/v2")
    public DeferredResult<String> helloGetDeferredResult() throws Exception {
        DeferredResult<String> deferredResult = new DeferredResult<>();

        //先存起来，等待触发
        synchronized (deferredResult) {
            deferredResultList.add(deferredResult);
        }
        return deferredResult;
    }

    @GetMapping("/testHeader")
    public Object testHeader(@RequestHeader("Accept-Encoding") String encoding,
                             @RequestHeader("Accept-Encoding") List<String> encodingList) {
        System.out.println(encoding);
        System.out.println(encodingList);
        return encoding;
    }


    @GetMapping("/setHelloToAll")
    public void helloSet() throws Exception {
        // 让所有hold住的请求给与响应
        deferredResultList.forEach(d -> d.setResult("say hello to all"));
    }

    @GetMapping("/hello")
    public Callable<String> helloGet() throws Exception {
        System.out.println(Thread.currentThread().getName() + " 主线程start");

        Callable<String> callable = () -> {
            System.out.println(Thread.currentThread().getName() + " 子子子线程start");
            TimeUnit.SECONDS.sleep(5); //模拟处理业务逻辑，话费了5秒钟
            System.out.println(Thread.currentThread().getName() + " 子子子线程end");

            // 这里稍微小细节一下：最终返回的不是Callable对象，而是它里面的内容
            return "hello world";
        };

        System.out.println(Thread.currentThread().getName() + " 主线程end");
        return callable;
    }

    @GagakuaiEncript(enabled = false)
    @PostMapping("/encript")
    public BaseResponseApapter testDespt() {

        return BaseResponseApapter.ok("成功");
    }


    @GetMapping("/test/curruser")
    public Object testCurrUser(@CurrUser CurrUserVo currUser) {
        return currUser;
    }
    @GetMapping("/test/curruser/map")
    public Object testCurrUserMap(@CurrUser Map<String,Object> currUser) {
        return currUser;
    }
    @GetMapping("/test/curruser/object")
    public Object testCurrUserObject(@CurrUser Object currUser) {
        return currUser;
    }

}